package editorarquivobmg.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ManipuladorDados {

    public static byte[] obterBytesIntervalo(byte[] data, int posInicial, int tamanho) {
        byte[] temp = new byte[tamanho];
        for (int i = 0; i < tamanho; i++) {
            temp[i] = data[i + posInicial];
        }
        return temp;
    }

    public static byte[] obterBytesIntervalo(byte[] data, int posInicial) {
        byte[] b = new byte[data.length - posInicial];
        for (int i = 0; i < data.length - posInicial; i++) {
            b[i] = data[i + posInicial];
        }
        return b;
    }

    public static byte[] concatenarArraysByte(byte[] A, byte[] B) {
        byte[] C = new byte[A.length + B.length];
        System.arraycopy(A, 0, C, 0, A.length);
        System.arraycopy(B, 0, C, A.length, B.length);
        return C;
    }

    public static int obterValorDecimal(byte[] data, int posInicial, int tamanho) {
        String str;
        byte[] b;
        char[] c, c2 = new char[tamanho * 2];
        char t;
        b = obterBytesIntervalo(data, posInicial, tamanho);
        str = byteArrayToHexString(b);
        c = str.toCharArray();
        for (int i = 0; i < tamanho * 2; i = i + 2) {
            t = c[i + 1];
            c[i + 1] = c[i];
            c[i] = t;
        }
        for (int i = 0, j = tamanho * 2 - 1; i < tamanho * 2; i++, j--) {
            c2[i] = c[j];
        }
        str = new String(c2);
        return hex2decimal(str);
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static byte[] inverterBytes(byte[] b) {
        byte[] x = new byte[b.length];
        for (int i = 0, j = b.length - 1; i < b.length; i++, j--) {
            x[i] = b[j];
        }
        return x;
    }

    public static byte[] obterBytes(File file) throws IOException {
        int fileSize = (int) file.length();
        byte[] retVal = new byte[fileSize];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        while (in.read(retVal) != -1) {
        }
        in.close();
        return retVal;
    }

    public static String byteArrayToHexString(byte[] b) {
        String result = "";
        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result.toUpperCase();
    }

    public static int hex2decimal(String s) {
        String digits = "0123456789ABCDEF";
        s = s.toUpperCase();
        int val = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int d = digits.indexOf(c);
            val = 16 * val + d;
        }
        return val;
    }

    public static void gravarTamanho(byte[] data, int posInicial, int tamanho) {
        String str = Integer.toHexString(tamanho).toUpperCase();
        if (str.length() % 2 != 0) {
            str = "0" + str;
        }
        byte[] temp = hexStringToByteArray(str);
        temp = inverterBytes(temp);
        int l = 0;
        for (int i = posInicial; i < posInicial + temp.length; i++) {
            data[i] = temp[l];
            l++;
        }
    }

    public static int obterEndereco(byte[] pData, String str) throws Exception {
        int d = 0;
        byte[] b = str.getBytes("UTF-8");
        d = forcaBruta(b, pData);
        return d;
    }

    public static int forcaBruta(byte[] p, byte[] t) {
        int i, j, M = p.length, N = t.length;
        for (i = 0; i <= N - M; i++) {
            j = 0;
            while (j < M) {
                if ((t[i + j] == p[j]) && (j < M)) {
                    j++;
                } else {
                    break;
                }
            }
            if (j == M) {
                return i;
            }
        }
        return -1;
    }

    public static String intToHexString(int valor) {
        String aux = "";
        aux = Integer.toHexString(valor).toUpperCase();
        if (aux.length() % 2 != 0) {
            aux = "0" + aux;
        }
        return aux;
    }
}
